package com.wuzuqing.android.eightdiagrams.term;

import java.util.ArrayList;
import java.util.List;

public class SolarSeason {

    public int year;
    public int month;
    public ExactDate calendar;

    public SolarSeason(int year, int month) {
        this.year = year;
        this.month = month;
        calendar = ExactDate.fromYmd(year, month, 1);
    }

    public static SolarSeason fromYm(int y, int m) {
        return new SolarSeason(y, m);
    }

    public static SolarSeason fromDate(ExactDate date) {
        return fromYm(date.getYear(), date.getMonth() + 1);
    }


    public int getYear() {
        return this.year;
    }

    public int getMonth() {
        return this.month;
    }

    /**
     * 获取当月是第几季度
     *
     * @return number 季度序号，从1开始
     */
    public int getIndex() {
        return (int) Math.ceil(this.month / 3);
    }

    /**
     * 季度推移
     *
     * @param seasons 推移的季度数，负数为倒推
     * @return object 推移后的季度
     */
    public SolarSeason next(int seasons) {
        if (0 == seasons) {
            return fromYm(this.year, this.month);
        }
        ExactDate date = ExactDate.fromYmd(this.year, this.month, 1);
        date.setMonth(date.getMonth() + 3 * seasons);
        return fromDate(date);
    }

    /**
     * 获取本季度的月份
     *
     * @return Array 本季度的月份列表
     */
    public List<SolarMonth> getMonths() {
        List<SolarMonth> l = new ArrayList<>();
        int index = this.getIndex() - 1;
        for (int i = 0; i < 3; i++) {
            l.add(SolarMonth.fromYm(this.year, 3 * index + i + 1));
        }
        return l;
    }

    @Override
    public String toString() {
        return this.getYear() + ". " + this.getIndex();
    }

    public String toFullString() {
        return this.getYear() + "年 " + this.getIndex() + "季度 ";
    }
}